/*
 * DateParameterValidator.java July 2009
 *
 * Copyright (C) 2009, Quirino Zagarese <quirino@laszloitalia.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 */
package org.eu4ria.parameters;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;

import org.eu4ria.exceptions.IllegalParameterValueException;

public class DateParameterValidator extends ParameterValidator{

	private java.sql.Date minvalue;

	private java.sql.Date maxvalue;

	private boolean includeMin;

	private boolean includeMax;

	private boolean evaluate(long time) {
		if(includeMax&&includeMin){
			return ((time>=minvalue.getTime())&&(time<=maxvalue.getTime()));
		}else if(includeMax&&(!includeMin)){
			return ((time>minvalue.getTime())&&(time<=maxvalue.getTime()));
		}else if((!includeMax)&&includeMin){
			return ((time>=minvalue.getTime())&&(time<maxvalue.getTime()));
		}else{
			return ((time>minvalue.getTime())&&(time<maxvalue.getTime()));
		}

	}

	public boolean isMaxIncluded() {
		return includeMax;
	}

	public void setIncludeMax(boolean includeMax) {
		this.includeMax = includeMax;
	}

	public boolean isMinIncluded() {
		return includeMin;
	}

	public void setIncludeMin(boolean includeMin) {
		this.includeMin = includeMin;
	}

	public Date getMaxvalue() {
		return maxvalue;
	}

	public void setMaxvalue(java.sql.Date maxvalue) {
		this.maxvalue = maxvalue;
	}

	public Date getMinvalue() {
		return minvalue;
	}

	public void setMinvalue(java.sql.Date minvalue) {
		this.minvalue = minvalue;
	}


	@Override
	public void acceptsValue(Object o, Class type) throws IllegalParameterValueException {
		Date date = new Date();
		long time = 0;
		try {
			time = Long.parseLong((String) o);
		} catch (NumberFormatException e) {
			throw new IllegalParameterValueException("Cannot parse value "
					+ o.toString() + " to a date.");
		}
		date.setTime(time);
		if ((maxvalue == null) || (minvalue == null)){
			return;
		}
		if(!evaluate(date.getTime())){
			throw new IllegalParameterValueException("Value out of specified range.");
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object getJavaValue(String string) {
		long time = Long.parseLong(string);
		Class[] parameterTypes = new Class[1];
		Object[] params = new Object[1];
		parameterTypes[0] = long.class;
		params[0] = time;

		Object date = null;
		try {
			Constructor c = this.getJavaType().getConstructor(parameterTypes);
			date = c.newInstance(params);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		return date;
	}

}
